package com.example.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.Result;
import com.example.dto.BlogSettingDTO;
import com.example.entity.User;
import com.example.handlers.BaseContext;
import com.example.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/blog/setting")
public class SettingUpdateController {

    @Resource
    private UserService userService;

    @PostMapping("detail")
    public Result<Object> detail() {
        return Result.success();
    }


    @PostMapping("/update")
    public Result<Object> update(@RequestBody BlogSettingDTO blogSettingDTO) {
        // 获取当前用户信息
        //获取线程变量
        Integer adminId = BaseContext.getCurrentId();

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getAdminId, adminId);
        User user = userService.getOne(wrapper);

        if (user == null) {
            return Result.error("用户信息不存在");
        }

        // 更新用户信息
        user.setNickname(blogSettingDTO.getAuthor());
        user.setIntroduction(blogSettingDTO.getIntroduction());
        user.setAvatar(blogSettingDTO.getAvatar());
        user.setGithubHomepage(blogSettingDTO.getGithubHomepage());
        user.setCsdnHomepage(blogSettingDTO.getCsdnHomepage());
        user.setGiteeHomepage(blogSettingDTO.getGiteeHomepage());
        user.setZhihuHomepage(blogSettingDTO.getZhihuHomepage());

        // 保存更新
        boolean success = userService.updateById(user);
        if (!success) {
            return Result.error("更新失败");
        }

        return Result.success();
    }
}
